<?php
namespace Home\Controller;

class PzgController extends HomeController{
    
    public function index(){
        valid_login();        
        $this->assign('category_tree', $this->getCategoryTree());
        $this->display();
    }
    
    public function pzgSave(){
         if(IS_POST){
            $title=I('post.title');
            $category_id=I('post.category_id');
            $price_start=I('post.price_start');
            $price_end=I('post.price_end');
            $description=I('post.description');
        
            $security_7days=I('post.security_7days',0);
            $security_delivery=I('post.security_delivery',0);
        
            
            $pzg_image_file=I('post.pzg_image_file');
            $pzg_movie_file=I('post.pzg_movie_file');
        
            $M_product = M('product');
            $M_shop = M('shop');
            $M_product_image = M('product_image');
        
            $data['title'] = $title;
            $data['category_id'] = $category_id;
            $data['price_start'] = $price_start;
            $data['price_end'] = $price_end;
            $data['description'] = $description;
            $data['security_7days'] = $security_7days;
            $data['security_delivery'] = $security_delivery;
        
            $data['uid'] = UID;
            $data['status'] = 1;
            !preg_match('/[\x{4e00}-\x{9fa5}A-Za-z0-9-_.\/\s]{4,60}+$/u', $data['title']) && $this->error( '商品名称长度必须在5-60个字符之间');
            !get_table_field($data['category_id'], 'id', 'id', 'product_category') && $this->error('商品分类为空或没有找到匹配的分类！');
            !is_numeric($data['price_start']) && $data['price_start'] < 0 && $this->error( '请输入商品价格，不能小于1');
            !is_numeric($data['price_end']) && $data['price_end'] < 0 && $this->error( '请输入商品价格，不能小于1');
            $data['price_start'] > $data['price_end'] && response(-1, '', '价格区间：最低价格不能高于最高价格');
            
          
            !getStatus('security_7days', $data['security_7days']) && $this->error('支持7天无理由退货不能为空');
            !getStatus('security_delivery', $data['security_delivery']) && $this->error( '承诺发货时间不能为空');

            $data['type'] = 'paizhao';
            $data['create_time'] = NOW_TIME;
            $data['update_time'] = NOW_TIME;
        
            $id = $M_product->add($data);
        
            if($id){
                $pics = I('post.pzg_image_file');        
                foreach ($pics as $pic) {
                    $M_product_image->add(array('product_id'=>$id, 'cover_id'=>$pic));
                }
        
                $movie = I('post.pzg_movie_file');
                $M_product->where(array('id'=>$id))->save(array('movie'=>$movie));
        
                $this->error('发布成功');
            }
            else{
                $this->error('发布失败');
            }
         }
    }
    
    /**
     * 获取商品分类树
     *
     * @return Ambigous <multitype:, mixed>
     */
    private function getCategoryTree(){
        static $list = array();    
        if (empty($list)) {
            $list = S('CACHE_ADMIN_PRODUCT_CATEGORY');
        }
    
        if (empty($list)) {
            $M_product_category = M('product_category');
            $list = $M_product_category->order('sort ASC')->select();
            $list = list_to_tree($list);
    
            S('CACHE_ADMIN_PRODUCT_CATEGORY', $list);
        }    
        return $list;
    }
}
?>